package com.threeox.commonlibrary.entity.engine.request.base;

import com.alibaba.fastjson.JSONObject;
import com.threeox.commonlibrary.entity.base.BaseObj;
import com.threeox.commonlibrary.util.RegexHelper;
import com.threeox.utillibrary.util.EmptyUtils;
import com.threeox.utillibrary.util.res.StringHelper;

/**
 *
 * @ClassName: BaseRequestMsg
 *
 * @Description: TODO(基本的请求对象)
 *
 * @author 赵屈犇
 *
 * @date 创建时间: 2017/7/4 12:18
 *
 * @version 1.0
 *
 */
public class BaseRequestMsg extends BaseObj {

    protected final String TAG = this.getClass().getSimpleName();

    private String key;// 由子类传递的唯一标志,用于在
    private String msgKey; // 解析Key 可以使用.号分割
    private String requestType;// 用户自定义的类型
    private JSONObject replaceParam; // 返回结构代替的参数
    private JSONObject requestParam; // 请求的参数
    private Class resultClass; //结果数据的Class,默认是JSONObject
    private String resultClassName; //结果数据的Class全名称,默认是JSONObject
    private String requestParamModelName;// 请求参数的模型名称

    private JSONObject replaceParamKeys;//返回结果代替的主键集合
    private JSONObject requestParamKeys;//请求参数代替的主键集合

    public void initData(StringHelper stringHelper, String key) {
        this.key = stringHelper.getString(key);
        this.msgKey = stringHelper.getStringText(msgKey);
        RegexHelper.replaceJSONByRawName(requestParamModelName, requestParam);
        this.replaceParam = RegexHelper.replaceSys(replaceParam);
        this.requestParam = RegexHelper.replaceSys(requestParam);
        try {
            if (EmptyUtils.isNotEmpty(resultClassName)) {
                this.resultClassName = stringHelper.getStringText(resultClassName);
                this.resultClass = Class.forName(resultClassName);
            } else {
                this.resultClass = JSONObject.class;
            }
        } catch (Exception e) {
            this.resultClass = JSONObject.class;
        }
        if (EmptyUtils.isEmpty(this.key) && EmptyUtils.isNotEmpty(key))
            this.key = key;
        this.requestType = stringHelper.getStringText(requestType);

        this.replaceParamKeys = RegexHelper.getReplaceKeys(replaceParam);
        this.requestParamKeys = RegexHelper.getReplaceKeys(requestParam);
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getMsgKey() {
        return msgKey;
    }

    public void setMsgKey(String msgKey) {
        this.msgKey = msgKey;
    }

    public JSONObject getReplaceParam() {
        return replaceParam;
    }

    public void setReplaceParam(JSONObject replaceParam) {
        this.replaceParam = replaceParam;
    }

    public JSONObject getRequestParam() {
        if (requestParam == null) {
            requestParam = new JSONObject();
        }
        return requestParam;
    }

    public void setRequestParam(JSONObject requestParam) {
        this.requestParam = requestParam;
    }

    public Class getResultClass() {
        return resultClass;
    }

    public void setResultClass(Class resultClass) {
        this.resultClass = resultClass;
    }

    public String getResultClassName() {
        return resultClassName;
    }

    public void setResultClassName(String resultClassName) {
        this.resultClassName = resultClassName;
    }

    public String getRequestType() {
        return requestType;
    }

    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }

    public String getRequestParamModelName() {
        return requestParamModelName;
    }

    public void setRequestParamModelName(String requestParamModelName) {
        this.requestParamModelName = requestParamModelName;
    }

    /**
     * 新增请求参数
     *
     * @param key
     * @param val
     */
    public void putRequestParam(String key, Object val) {
        if (this.requestParam == null) {
            this.requestParam = new JSONObject();
        }
        this.requestParam.put(key, val);
    }

    /**
     * 得到请求参数 根据key
     *
     * @param key
     * @return
     */
    public <T extends Object> T  getRequestParam(String key) {
        if (this.requestParam != null) {
            return (T) this.requestParam.get(key);
        }
        return null;
    }

    /**
     * 删除请求参数 根据key
     *
     * @param key
     */
    public void removeRequestParam(String key) {
        if (this.requestParam != null) {
            this.requestParam.remove(key);
        }
    }

    /**
     * 新增替换参数
     *
     * @param key
     * @param val
     */
    public void putReplaceParam(String key, Object val) {
        if (this.replaceParam == null) {
            this.replaceParam = new JSONObject();
        }
        this.replaceParam.put(key, val);
    }

    /**
     * 得到替换参数 根据参数
     *
     * @param key
     * @return
     */
    public Object getReplaceParam(String key) {
        if (this.replaceParam != null) {
            return this.replaceParam.get(key);
        }
        return this.replaceParam;
    }

    /**
     * 初始化替換參數
     *
     * @param intent
     *
     * public void initReplaceParam(Intent intent) {
     *  RegexHelper.replace(this.replaceParam, this.replaceParamKeys, intent);
     * }
     */

    /**
     * 初始化替換參數
     *
     * @param data
     */
    public void initReplaceParam(Object data) {
        RegexHelper.replace(this.replaceParam, this.replaceParamKeys, data, true);
    }

    /**
     * 初始化請求參數
     *
     * @param intent
     *
     * public void initRequestParam(Intent intent) {
     *  RegexHelper.replace(this.requestParam, this.requestParamKeys, intent);
     * }
     */

    /**
     * 初始化請求參數
     *
     * @param data
     */
    public void initRequestParam(Object data) {
        RegexHelper.replace(this.requestParam, this.requestParamKeys, data);
    }

}
